package com.cn.zengzeng.spbzengzeng.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;


public class MyDbutils {
	private static DruidDataSource dataSource ;
	private MyDbutils() {
		throw new IllegalStateException("Utility class");
	}
	static {
		try {
			Properties prop = new Properties() ;
			InputStream inputStream = MyDbutils.class.getClassLoader().
							getResourceAsStream("druid.properties") ;
			prop.load(inputStream);
			dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static DataSource getDataSource() {
		return dataSource ;
	}
	
		public static void close(ResultSet rs,Statement stmt,Connection conn) {
			if(rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		public static void close(Statement stmt,Connection conn){
			close(null, stmt, conn);
		}

}

